package com.jf.cloud.platform.controller.platform;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.platform.dto.InformationDTO;
import com.jf.cloud.platform.model.Information;
import com.jf.cloud.platform.service.InformationService;
import com.jf.cloud.platform.vo.InformationVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 
 *
 * @author zz
 * @date 2023-03-06 09:14:00
 */
@RestController("platformInformationController")
@RequestMapping("/p/information")
@Tag(name = "资讯管理")
public class InformationController {

    @Autowired
    private InformationService informationService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取资讯列表", description = "分页获取资讯列表")
    public ServerResponseEntity<PageVO<InformationVO>> page(@Valid PageDTO pageDTO,@ParameterObject InformationDTO informationDTO) {
        PageVO<InformationVO> informationPage = informationService.pageList(pageDTO,informationDTO);
        return ServerResponseEntity.success(informationPage);
    }

    @GetMapping
    @Operation(summary = "获取资讯", description = "根据id获取资讯")
    public ServerResponseEntity<Information> getById(@RequestParam Long id) {
        return ServerResponseEntity.success(informationService.getById(id));
    }

    @PostMapping
    @Operation(summary = "保存资讯", description = "保存资讯")
    public ServerResponseEntity<Void> save(@Valid @RequestBody InformationDTO informationDTO) {
        Information information = mapperFacade.map(informationDTO, Information.class);
        information.setId(null);
        informationService.save(information);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新资讯", description = "更新资讯")
    public ServerResponseEntity<Void> update(@Valid @RequestBody InformationDTO informationDTO) {
        Information information = mapperFacade.map(informationDTO, Information.class);
        informationService.update(information);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除资讯", description = "根据id删除资讯")
    public ServerResponseEntity<Void> delete(@RequestParam Long id) {
        informationService.deleteById(id);
        return ServerResponseEntity.success();
    }
}
